<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="xingxing" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- tree结构 -->
        <!-- :props="{label: 'name'}" -->
        <el-tree default-expand-all :data="list">
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="xingxing" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加的弹框 -->
    <el-dialog
      :title="isEdit ? '编辑':'添加'"
      :visible.sync="showDialog"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit
        v-if="showDialog"
        :id="currentId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccsee"
        @close="hClose"
      />
    </el-dialog>
    <!-- 编辑的的弹框 -->
    <!-- <el-dialog
      title="操作或添加"
      :visible.sync="showDialogEdit"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit
        v-if="showDialogEdit"
        :id="currentId"
        :is-edit="isEdit"
        @success="hSuccsee"
      />
    </el-dialog> -->
  </div>
</template>

<script>
import { deleteDepartById, getDepartmentList } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptsDialog.vue'
export default {
  components: { addOrEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      // showDialogEdit: false,
      currentId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    async loadDepartmentList() {
      const res = await getDepartmentList()
      res.data.depts.shift()
      // console.log(company)
      this.list = tranListToTreeData(res.data.depts)
      // 初级写法
      /* this.originList = res.data.depts.map(item => {
        return { id: item.id, pid: item.pid, name: item.name, code: item.code }
      }) */
      // 高级写法 直接结构item
      this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
    },
    hShowAdd(id) {
      this.showDialog = true
      // console.log(data)
      this.currentId = id
      this.isEdit = false
    },
    hShowEdit(id) {
      this.showDialog = true
      // console.log(data)
      this.currentId = id
      this.isEdit = true
    },
    hSuccsee() {
      this.showDialog = false
      this.showDialogEdit = false
      this.loadDepartmentList()
    },
    del(id) {
      this.$confirm('您确定要删除吗', '提示', { type: 'warning' })
        .then(async() => {
          try {
            const res = await deleteDepartById(id)
            this.$message.success(res.message)
            this.loadDepartmentList()
          } catch (e) {
            this.$message.error(e.message)
          }
        }).catch(() => {})
    },
    hClose() {
      this.showDialog = false
      this.isEdit = false
    }
  }
}
</script>
